package com.alog.yzd.jdbc.hm;

import com.alog.yzd.jdbc.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class UserDaoImpl extends AbstractDao {
    public int updateUser(User user) {
        String sql = "update user set name = ?, pwd = ?, birth = ?  where id = ?";
        ArrayList<Object> params = new ArrayList<>();
        params.add(user.getName());
        params.add(user.getPwd());
        params.add(user.getBirth());
        params.add(user.getId());
        return super.update(sql,params);
    }

    public int delete(User user){
        String sql = "delete from user where id < ?";
        ArrayList<Object> params = new ArrayList<>();
        params.add(user.getId());
        return super.update(sql, params);
    }

    public int addUser(User user){
        String sql = "insert into user (id, name, pwd, birth) values (?, ?, ?, ?)";
        ArrayList<Object> params = new ArrayList<>();
        params.add(user.getName());
        params.add(user.getPwd());
        params.add(user.getBirth());
        params.add(user.getId());
//        return super.insert(sql, params);
        return super.update(sql, params);
    }

    public User findUser(String name, String pwd){
        String sql = "select id, name, pwd, birth from user where name = ? and pwd = ?";
        ArrayList<Object> params = new ArrayList<>();
        params.add(name);
        params.add(pwd);
        User user = (User) super.find(sql, params);
        return user ;
    }

    @Override
    protected Object rowMapper(ResultSet rs) throws SQLException {
        User user = new User();
        user.setId(rs.getInt("id"));
        user.setPwd(rs.getString("pwd"));
        user.setName(rs.getString("name"));
        user.setBirth(rs.getDate("birth"));
        return user;
    }
}
